package com.sigmazero13.components;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MultiRowInput<T extends View> extends LinearLayout {

	private Context mContext;
	private String addLabel = "Add...";
	protected List<T> inputs;
	protected InputFactory<T> factory;
	protected boolean minimumOne = false;
	protected LinearLayout firstRow;
	protected T firstInput;
	
	protected interface InputFactory<V> {
		public V create(Context context);
	}
	
	public MultiRowInput (Context context) {
		super(context);
		mContext = context;
		commonSetup();
	}
	
	public MultiRowInput(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MultiRowInput);
		String newLabel = a.getString(R.styleable.MultiRowInput_add_label);
		if (newLabel != null) {
			addLabel = newLabel;
		}
		minimumOne = a.getBoolean(R.styleable.MultiRowInput_minimum_one, false);
		a.recycle();
		
		commonSetup();
	}
	
	private void commonSetup() {
		this.setOrientation(LinearLayout.VERTICAL);
		inputs = new ArrayList<T>();
		
		if (!minimumOne) {
			firstRow = new LinearLayout(mContext);
			firstRow.setOrientation(LinearLayout.HORIZONTAL);
			
			TextView tv = new TextView(mContext);
			tv.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
			tv.setText(addLabel);
			tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
			tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18.0f);
			firstRow.addView(tv);
			firstRow.addView(addNewInputButton());
			
			this.addView(firstRow);
		}
	}
	
	public void attachFactory(InputFactory<T> factory) {
		this.factory = factory;
		if (minimumOne && firstInput == null) {
			addFirstInput();
		}
	}
	
	public void deleteAllRows() {
		inputs.clear();
		while (this.getChildCount() > 1) {
			this.removeViewAt(1);
		}
		if (minimumOne) {
			this.removeViewAt(0);
		}
		firstRow = null;
		firstInput = null;
	}
	
	private ImageView addNewInputButton() {
		//ImageButton b = new ImageButton(mContext);
		ImageView b = new ImageView(mContext);
		b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		b.setImageResource(R.drawable.input_add);
		b.setPadding(0, 0, 0, 0);
		b.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View clickView) {
				addNewInput();
			}
		});
		
		return b;
	}
	
	private T addFirstInput() {
		firstRow = new LinearLayout(mContext);
		firstRow.setOrientation(LinearLayout.HORIZONTAL);
		
		firstInput = factory.create(this.getContext());
		inputs.add(firstInput);
		firstRow.addView(firstInput);
		firstRow.addView(addNewInputButton());
		
		this.addView(firstRow);
		return firstInput;
	}
	
	public T addNewInput() {
		T newInput;
		if (minimumOne && firstInput == null) {
			newInput = addFirstInput();
		} else {
			LinearLayout newRow = new LinearLayout(mContext);
			newRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			newRow.setOrientation(LinearLayout.HORIZONTAL);
			
			newInput = factory.create(this.getContext());
			inputs.add(newInput);
			
			ImageView b = new ImageView(mContext);
			b.setImageResource(R.drawable.input_delete);
			b.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View clickView) {
					LinearLayout parent = (LinearLayout)clickView.getParent();
					View delView = parent.getChildAt(0);
					inputs.remove(delView);
					MultiRowInput.this.removeView(parent);
				}
			});
			
			newRow.addView(newInput);
			newRow.addView(b);
			
			this.addView(newRow);
		}
			
		return newInput;
	}
	
	public List<T> getAllInputs() {
		return inputs;
	}
}
